class StudentMgrController {
    def createBlogService
    def studentSearcheService

    // 开启和停用学生
    def goswitch = {
        if (params.id) {
            def student = Student.get(params.id)
            student.enabled = !student.enabled
            student.save(flush: true)
        }
        redirect(action: "list" , controller: "student")
    }

    def classesStudents = {
        Classes c = Classes.get(params.id)
        def classesStudentList = Student.findAllByClasses(c,[sort:"name",order:"desc"])
        log.info(classesStudentList.size())
        // todo 此处的classes应该是某一个学校的，这里在没有Session的状况下就用全部
        [classesStudentList:classesStudentList,classesInstance:c]
    }

    def classesStudentsEvaluate = {
        Classes c = Classes.get(params.id)
        def classesStudentList = Student.findAllByClasses(c,[sort:"name",order:"desc"])
        log.info(classesStudentList.size())
        // todo 此处的classes应该是某一个学校的，这里在没有Session的状况下就用全部
        [classesStudentList:classesStudentList,classesInstance:c]
    }

    def createblog = {
          def studentInstance 
          if (params.id) {
            studentInstance = Student.get(params.id)
        }else{
             flash.message = "没有找到此学生！"
             return redirect(action: "classesStudents")
        }
         if(createBlogService.sendCreateBlog(studentInstance,grailsApplication.config.grails.default.blog.create.uri)){
                studentInstance.hasBlog = true
                studentInstance.save(flush:true)
                flash.message = "创建学生博客成功！"
         }else{
                flash.message = "创建学生博客失败，请重试或联系管理员！"
         }
         return redirect(action: "classesStudents",id:studentInstance.classes.id)
     }

    def index = {
        redirect(action: "list", params: params)
    }

    // 某学校下的所有学生
    def list = {
        def studentLevels = org.grails.plugins.lookups.Lookup.codeList("studentLevel")
        params.max = Math.min(params.max ? params.int('max') : 10, 100)
        [studentInstanceList: Student.findAllBySchoolAndState(session.school,false,params), studentInstanceTotal: Student.countBySchoolAndState(session.school,false),studentLevels:studentLevels]
    }
    // 查看某学生的情况
    def show = {
        def studentInstance = Student.get(params.id)
        if (!studentInstance) {
            flash.message = "没有找到此学生"
            if(!params.searchFlag){
                    return redirect(action: "list")
              }else{
                    redirect(action: "studentSearchList")
              }
        }
        else {
            [studentInstance: studentInstance]
        }
    }

    // 学员查询
     def studentSearchList = {
        if(params.isSearch){
            studentSearcheService.searcheStudentInit(params.name,params.sex,params.level,Classes.get(params.classesid),session.school)
        }
        return studentSearcheService.searchStudentList()
//        return render(view:"list",model:studentSearcheService.searchStudentList())
     }

    //快速的添加学生的方法
    def create = {

        def studentInstance = new Student()
        studentInstance.properties = params
        
        return [studentInstance: studentInstance]
    }

    //快速的添加学生的具体保存方法
    def save = {
        def studentInstance = new Student(params)
        studentInstance.school = session.school
        
        if (studentInstance.save(flush: true)) {
            flash.message = "创建学生${studentInstance.name}成功！"
            return redirect(action: "create")
        }else {
            render(view: "create", model: [studentInstance: studentInstance])
        }
    }

    //快速的添加学生的具体保存方法
    def newStudentList = {
        [newsStudentList:Student.findAllBySchoolAndClasses(session.school,null)]
    }

}
